package com.ptswitch.material.model;

import java.io.Serializable;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.OrderBy;

import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;

@Entity
@org.hibernate.annotations.Entity(dynamicInsert = true, dynamicUpdate = true)
public class Bom implements Serializable {

	private static final long serialVersionUID = 6705569910023553421L;
	@Id
	@GeneratedValue(generator = "hibernateuuid")
	@GenericGenerator(name = "hibernateuuid", strategy = "uuid")
	@Column(length = 32)
	private String id;
	
	@OneToMany(mappedBy = "bom", fetch = FetchType.LAZY)
	@OrderBy(value="code")
	@Cascade(value={CascadeType.DELETE})
	private List<BomMaterial> bomMaterials;
	
	@OneToOne(fetch = FetchType.EAGER)
	@NotFound(action = NotFoundAction.IGNORE)
//	@Cascade(value={CascadeType.DELETE,CascadeType.PERSIST,CascadeType.SAVE_UPDATE})
	private Upload upload;
	
	@Column(columnDefinition="varchar(32) default ''")
	private String code;
	@Column(columnDefinition="varchar(2048) default ''")
	private String version;
	@Column(columnDefinition="varchar(2048) default ''")
	private String name;
	
	@Column(columnDefinition="varchar(2048) default ''")
	private String fileName;
	@Column(columnDefinition="varchar(2048) default ''")
	private String remark;
	
//	@PrePersist
//	@PreUpdate
//	public void escape(){
//		OgnlUtils.escapeStringProperty(this);
//	}


	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public List<BomMaterial> getBomMaterials() {
		return bomMaterials;
	}

	public void setBomMaterials(List<BomMaterial> bomMaterials) {
		this.bomMaterials = bomMaterials;
	}

	public Upload getUpload() {
		return upload;
	}

	public void setUpload(Upload upload) {
		this.upload = upload;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}
	
	public String getVersion() {
		return version;
	}

	public void setVersion(String version) {
		this.version = version;
	}

	public String toString() {
		return this.code + "::" + this.version;
	}

	public boolean equals(Object o){
		if(o!=null && o instanceof Bom){
			return this.code.equals(((Bom)o).getCode()) && this.version.equals(((Bom)o).getVersion());
		}
		return false;
	}

}
